<?php
class Blacklist {
	private $name;
	private $url;
	private $replies;

	public function __construct($name, $url) {
		$this->name = $name;
		$this->url = $url;
		$this->replies = array();
	}

	// Accessors
	public function getName() { return $this->name; }

	public function getURL() { return $this->url; }

	// Methods
	public function checkBL($ip) {
		$checkRecord = $this->reverseIP($ip) . '.' . $this->url;

		if (checkdnsrr($checkRecord, 'A') === true) {
			$returnArr = dns_get_record($checkRecord, DNS_A);

			$returnArr = explode('.', $returnArr[0]['ip']);

			if (($description = $this->getDescriptionFromReply($returnArr[3])) !== false) {
				if (checkdnsrr($checkRecord, 'TXT')) {
					$arr = dns_get_record($checkRecord, DNS_TXT);

					if (is_array($arr) && isset($arr['txt'])) {
						$txt = $arr['txt'];
					}
					else {
						$txt = 'No TXT Record';
					}
				}

				return array('numeric'=>$returnArr[3], 'description'=>$description, 'txt'=>$txt);
			}
		}

		return false;
	}

	public function addReply($numeric, $description) {
		$this->replies[] = new Reply($numeric, $description);
	}

	public function getDescriptionFromReply($numeric) {
		foreach ($this->replies as $reply) {
			if ($reply->getNumeric() == $numeric) {
				return $reply->getDescription();
			}
		}

		return false;
	}

	private function reverseIP($ip) {
		$arr = explode('.', $ip);
		$arr = array_reverse($arr);

		return implode('.', $arr);
	}
}
?>